package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.HospitalImg;
import team.yidong.fenda.repository.HospitalImgRepository;
import team.yidong.fenda.service.HospitalImgService;

import java.util.List;

/**
 * Created by xuanlong on 2017/11/17.
 */
@Service
public class HospitalImgServiceImpl implements HospitalImgService {

    @Autowired
    HospitalImgRepository hospitalImgRepository;

    @Override
    public JSONArray findByHospital(Integer hospitalId) {
        List<HospitalImg> hospitalImgList = hospitalImgRepository.findByHospital(hospitalId);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospital"});
        return JSONArray.fromObject(hospitalImgList, jsonConfig);
    }

    @Override
    public HospitalImg findById(Integer id) {
        return hospitalImgRepository.findOne(id);
    }

    @Transactional
    @Override
    public void updateHospitalImg(HospitalImg hospitalImg) {
        hospitalImgRepository.save(hospitalImg);
    }

    @Transactional
    @Override
    public void deleteImg(Integer imgId) {

        hospitalImgRepository.delete(imgId);
    }
}
